<template>
  <div>
    <!-- 面包屑视图 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 添加商品按钮区域 -->
      <el-row>
        <el-button
          data="commoditylist"
          type="primary"
          @click="getcommodelVisible()"
          >添加分类</el-button
        >
      </el-row>
      <tree-table
        :data="commoditylist"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
        border
        :show-row-hover="false"
      >
        <template slot="like" slot-scope="scope">
          <i
            class="el-icon-success"
            v-if="scope.row.cat_deleted === false"
            style="color:green;"
          ></i>
          <i class="el-icon-error" v-else style="color:red;"></i>
        </template>
        <template slot="olike" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag type="success" v-else-if="scope.row.cat_level === 1"
            >二级</el-tag
          >
          <el-tag type="info" v-else>三级</el-tag>
        </template>
        <template slot="clike">
          <!-- 修改 -->
          <el-button type="primary" icon="el-icon-edit" size="mini">
            编辑
          </el-button>
          <!-- 删除 -->
          <el-button type="danger" icon="el-icon-delete" size="mini">
            删除
          </el-button>
        </template>
      </tree-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="commodityinfo.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="commodityinfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <el-dialog
        title="添加分类"
        :visible.sync="commodelVisible"
        width="50%"
        @close="commodelclose"
      >
        <el-form
          ref="commodelformref"
          :model="addcomform"
          label-width="100px"
          :rules="getaddcomform"
        >
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="addcomform.cat_name"></el-input>
          </el-form-item>
          <el-form-item label="父级分类">
            <el-cascader
              :props="expandT"
              v-model="addmonform"
              :options="addcomformlist"
              @change="comhandleChange"
              clearable
            ></el-cascader>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="commodelVisible = false">取 消</el-button>
          <el-button type="primary" @click="commodelclick">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 添加商品分类的的显示与隐藏
      commodelVisible: false,
      //   添加分类列表的表单数据对象
      addcomform: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      //  父类级分类的表单数据对象
      expandT: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        expandTrigger: 'hover',
        checkStrictly: 'true'
      },
      //   选中id的父级分类数组
      addmonform: [],
      //   添加按钮获取的数据
      addcomformlist: [],
      // 验证添加用户分类表单的验证规则
      getaddcomform: {
        cat_name: [{ required: true, message: '请输入用户名', trigger: 'blur' }]
      },

      commodityinfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },

      // 获取所有的分类商品列表
      commoditylist: [],
      //   数据总条数
      total: 0,
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'like'
        },
        {
          label: '排序',
          type: 'template',
          template: 'olike'
        },
        {
          label: '操作',
          type: 'template',
          template: 'clike'
        }
      ]
    }
  },
  created() {
    this.getcommoditylist()
  },
  methods: {
    async getcommoditylist() {
      const { data: res } = await this.$http.get('categories', {
        params: this.commodityinfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品分类失败')
      }
      //   console.log(res)
      //   把数据列表赋值给commoditylist
      this.commoditylist = res.data.result

      this.total = res.data.total
      console.log(this.total)
    },
    // 监听pagesize的改变
    handleSizeChange(newsize) {
      this.commodityinfo.pagesize = newsize
      this.getcommoditylist()
    },
    // 监听pagesize的改变
    handleCurrentChange(newpage) {
      this.commodityinfo.pagenum = newpage
      this.getcommoditylist()
    },
    // 点击分类添加的按钮
    getcommodelVisible() {
      // 先获取父级分类的列表
      this.getcommodity()
      //   this.$refs.addcomform.validate(async valid => {
      //     if (!valid) return
      //     const { data: res } = await this.$http.post(
      //       'categories' + this.addcomform.id
      //     )
      //     console.log(res)
      //   })
      this.commodelVisible = true
    },
    async getcommodity() {
      const { data: res } = await this.$http.get('categories', {
        params: { type: 2 }
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取添加列表失败')
      }
      console.log(res.data)
      this.addcomformlist = res.data
    },
    // 选择项发生变化选中这项
    comhandleChange() {
      //  级联菜单中选择项发生变化时触发
      // console.log(this.addmonform)
      //  如果用户选择了父级分类
      if (this.addmonform.length > 0) {
        // 则将数组中的最后一项设置为父级分类
        this.addcomform.cat_pid = this.addmonform[this.addmonform.length - 1]
        // level也要跟着发生变化
        this.addcomform.cat_level = this.addmonform.length
        return
      } else {
        this.addcomform.cat_pid = 0
        this.addcomform.cat_level = 0

        return
      }
    },
    // 点击确定按钮
    commodelclick() {
      this.$refs.commodelformref.validate(async valid => {
        if (!valid) {
          return
        }
        const { data: res } = await this.$http.post(
          'categories',
          this.addcomform
        )
        if (res.meta.status !== 201) {
          return this.$message.error('添加分类失败')
        }

        this.$message.success('添加分类成功')
        this.getcommoditylist()
        this.commodelVisible = false
      })

      // console.log(this.addcomform)
    },
    commodelclose() {
      this.$refs.commodelformref.resetFields()
      this.addmonform.length = []
      this.addcomform.cat_pid = 0
      this.addcomform.cat_level = 0
    }
  }
}
</script>
<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
